﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Orf.Model;
using Orf.dto;

namespace Orf_system.收费业务
{
    public partial class FrmTuifei : Form
    {
        tuiFeiDAO tfDAO = null;
        string cfhm = "", sfxm = "";
        public FrmTuifei()
        {
            InitializeComponent();
            tfDAO = new tuiFeiDAO();
        }

        private void Get_TuifeiInfo()
        {
            MZ_CF01 cf01Class = new MZ_CF01();
            cf01Class.fPHM = txtFphm.Text;
            DataTable dt_BrInfo = null, dt_BrCfInfo = null;
            try
            {
                dt_BrInfo = tfDAO.Get_TuifeiInfo(cf01Class, 1);
                dt_BrCfInfo = tfDAO.Get_TuifeiInfo(cf01Class, 2);
                if (dt_BrInfo.Rows.Count > 0)
                {
                    txtBrName.Text = dt_BrInfo.Rows[0][0].ToString();
                    txtDocName.Text = dt_BrInfo.Rows[0][1].ToString();
                    txtDeptName.Text = dt_BrInfo.Rows[0][2].ToString();
                }

                int intRows = dt_BrCfInfo.Rows.Count;
                int intColumns = dt_BrCfInfo.Columns.Count;
                for (int i = 0; i < intRows; i++)
                    dgvCfInfo.Rows.Add();
                for (int i = 0; i < intRows; i++)
                {
                    for (int j = 0; j < intColumns; j++)
                    {
                        dgvCfInfo.Rows[i].Cells[j + 1].Value = dt_BrCfInfo.Rows[i][j].ToString();

                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }

        private void txtFphm_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (dgvCfInfo.Rows.Count > 0)
                    dgvCfInfo.Rows.Clear();
                Get_TuifeiInfo();
                dgvCfInfo.Focus();
            }
        }

        private void dgvCfInfo_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            double str = 0.00;
            if (e.ColumnIndex == 0)//定位点击第一列的单元格，才打上勾
            {
                if ((bool)this.dgvCfInfo.CurrentRow.Cells[0].EditedFormattedValue == false)//判断是否没有打上勾
                {
                    this.dgvCfInfo.CurrentRow.Cells[0].Value = true;//如果没有打上勾，点击的时候打钩
                    try
                    {

                        for (int i = 0; i < dgvCfInfo.Rows.Count; i++)
                        {
                            if (dgvCfInfo.Rows[i].Cells[0].Value != null)//判断第一列单元的的值是否为空
                            {
                                if (bool.Parse(dgvCfInfo.Rows[i].Cells[0].Value.ToString()) == true)
                                {

                                    str = Convert.ToDouble(dgvCfInfo.Rows[i].Cells[7].Value.ToString()) + str;//累加金额
                                    cfhm = this.dgvCfInfo.Rows[i].Cells[1].Value.ToString() + "," + cfhm;//获取处方号码
                                    sfxm = this.dgvCfInfo.Rows[i].Cells[2].Value.ToString() + "," + sfxm;//获取收费项目编码
                                }
                                txtTfJe.Text = str.ToString();//把金额显示在TextBox中
                            }
                        }

                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message.ToString());
                    }
                }
                else
                {
                    this.dgvCfInfo.CurrentRow.Cells[0].Value = false;
                    try
                    {
                        for (int i = 0; i < dgvCfInfo.Rows.Count; i++)
                        {
                            if (dgvCfInfo.Rows[i].Cells[0].Value != null)
                            {
                                if (bool.Parse(dgvCfInfo.Rows[i].Cells[0].Value.ToString()) == true)
                                {

                                    str = Convert.ToDouble(dgvCfInfo.Rows[i].Cells[7].Value.ToString()) + str;
                                    cfhm = this.dgvCfInfo.Rows[i].Cells[1].Value.ToString() + "," + cfhm;
                                    sfxm = this.dgvCfInfo.Rows[i].Cells[2].Value.ToString() + "," + sfxm;
                                }
                                txtTfJe.Text = str.ToString();
                            }
                        }

                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message.ToString());
                    }
                }
                
            
            }
                
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnTf_Click(object sender, EventArgs e)
        {
            int res = 0;
            MZ_CF01 cf01Class = new MZ_CF01();
            MZ_SFMX sfmxClass = new MZ_SFMX();
            cf01Class.fPHM = txtFphm.Text;
            sfmxClass.fPHM = txtFphm.Text;
            if (sfxm == "")
            {
                MessageBox.Show("请选择要退费的项目！");
            }
            try
            {
                if (MessageBox.Show("真的要将选择的项目进行退费吗！", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                {
                    string[] st = cfhm.Split(',');
                    string[] xm = sfxm.Split(',');
                    for (int i = 0; i < st.Length - 1; i++)
                    {
                        cf01Class.cFHM = st[i];
                        sfmxClass.sFXM = xm[i];
                        res = tfDAO.Update_Sfmx(sfmxClass);
                    }
                }

                if (res > 0)
                {
                    tfDAO.Update_Cf01(cf01Class);
                    MessageBox.Show("退费成功，金额为" + txtTfJe.Text);
                }
                else
                {
                    MessageBox.Show("退费失败！");
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }
    }
}
